<template>
  <div class="login-wrapper">
    <div class="login-container">
      <div class="form-header">
        <h2>用户登录</h2>
        <p>欢迎回来，请登录您的账号</p>
      </div>
      <form @submit.prevent="Login" class="floating-form">
        <div class="input-group">
          <input id="username" v-model.trim="loginForm.username" type="text" autocomplete="off" @input="validateInput"
            required />
          <label for="username">工号/电话号码</label>
          <span class="highlight"></span>
        </div>
        <div class="input-group">
          <input id="password" v-model.trim="loginForm.password" type="password" autocomplete="off"
            @input="validateInput" required />
          <label for="password">密码</label>
          <span class="highlight"></span>
        </div>
        <div class="error-message" v-if="errorMsg">{{ errorMsg }}</div>

        <div class="user-type">
          <el-radio-group v-model="userType">
            <el-radio value="admin">管理员</el-radio>
            <el-radio value="user">普通用户</el-radio>
          </el-radio-group>
        </div>

        <button type="submit" class="submit-btn" :disabled="!isFormValid">
          <span>登录</span>
          <i class="arrow-icon"></i>
        </button>
        <div class="form-footer">
          <span>还没有账号？</span>
          <a href="javascript:void(0);" @click="goToRegister">立即注册</a> <!-- 修改为使用点击事件 -->
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAccountStore } from '@/stores/accountInfo';
import { adminLogin, userLogin } from '@/api/login';
import { ElMessage } from 'element-plus'
const baseURL = import.meta.env.VITE_API_BASE

const router = useRouter();
const accountStore = useAccountStore();

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
});

const userType = ref('');
const errorMsg = ref('');
const isFormValid = ref(false);

// 验证输入
const validateInput = () => {
  if (loginForm.username && loginForm.password) {
    isFormValid.value = true;
    errorMsg.value = '';
  } else {
    isFormValid.value = false;
  }
};

// 登录处理
const Login = async () => {
  if (!userType.value) {
    ElMessage.warning('请选择用户类型')
    return
  }

  try {
    // 1) 调用对应的登录接口
    const data = userType.value === 'admin'
      ? await adminLogin({ employeeNumber: loginForm.username, password: loginForm.password })
      : await userLogin({ tele: loginForm.username, password: loginForm.password })

    // 2) 接口已在 request 拦截器中保证 code===0 才返回，这里直接拿 data
    if (userType.value === 'admin') {
      accountStore.setAdminInfo(data.data)                      // 保存管理员信息
    } else {
      accountStore.setUserInfo(data.data)                       // 保存普通用户信息
    }

    // 3) 跳转首页/封面页
    ElMessage.success('登录成功！')
    router.push('/cover')

  } catch (err) {
    // request 拦截器已经对非 0 码做了统一弹窗，这里只处理网络或异常错误
    ElMessage.error(err?.message || '登录失败，请重试')
    console.error(err)
  }
}

// 错误提示
const errorMessage = (text) => {
  errorMsg.value = text;
  setTimeout(() => {
    errorMsg.value = '';
  }, 3000);
};

// 注册跳转方法
const goToRegister = () => {
  router.push({ path: '/register' }); // 跳转到注册页面的路由
};

onMounted(() => {
  validateInput();
});
</script>








<style scoped>
.login-wrapper {
  min-height: 93.5vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 480px;
  background: white;
  border-radius: 20px;
  padding: 40px;
  padding-right: 80px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  color: #2c3e50;
  font-size: 32px;
  margin-bottom: 10px;
  font-weight: 700;
}

.form-header p {
  color: #95a5a6;
  font-size: 16px;
}

.floating-form .input-group {
  position: relative;
  margin-bottom: 30px;
}

.input-group input {
  width: 100%;
  padding: 15px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: transparent;
}

.input-group label {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  background: white;
  padding: 0 5px;
  color: #95a5a6;
  font-size: 16px;
  transition: all 0.3s ease;
  pointer-events: none;
}

.input-group input:focus,
.input-group input:valid {
  border-color: #3498db;
}

.input-group input:focus+label,
.input-group input:valid+label {
  top: 0;
  font-size: 14px;
  color: #3498db;
}

.submit-btn {
  width: 100%;
  padding: 15px;
  margin-left: 15px;
  background: linear-gradient(to right, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}

.arrow-icon {
  border: solid white;
  border-width: 0 2px 2px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(-45deg);
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #95a5a6;
}

.form-footer a {
  color: #3498db;
  text-decoration: none;
  margin-left: 5px;
  font-weight: 600;
}

.form-footer a:hover {
  text-decoration: underline;
}

.error-message {
  color: #f56c6c;
  font-size: 14px;
  text-align: center;
  margin-bottom: 20px;
}
</style>
